/*jslint vars: true, forin: true, sloppy: true */
/*global angular, probe, URLs */
probe.run(function (ocAPI, $http, $location) {
	window.$ = ocAPI;
	ocAPI.setAPIOrigin(URLs.probeDataRemoteAPIOrigin);

	ocAPI.interceptor('unsignedException', function (response) {
		var state = response.state;
		if (state === 2) {
			throw state;
		}
	});
	ocAPI.interceptor('login fail', function (res) {
		var state = res.state;
		if (state === 5) {
			throw state;
		}
	});

	ocAPI.API('origin', { //OK SUCCESS
		request: function () {
			var probeId = $location.absUrl().match(/([0-9a-fA-F]{40})\.html/)[1];
//			var probeId = '5bb3a268214087a94a70f25c088f5410f800ab6d';
			return $http.get(this.APIOrigin + probeId + "/path", {
				withCredentials: false
			});
		}
	});
	ocAPI.API('noop', { //OK SUCCESS
		request: function () {
			return $http.get(this.APIOrigin + "noop");
		},
		interceptor: ['unsignedException']
	});
	ocAPI.API('login', { //OK SUCCESS
		request: function (token) {
			return $http.post(this.APIOrigin + "login", {
				probeCode: $location.absUrl().match(/([0-9a-fA-F]{40})\.html/)[1],
//				probeCode: '5bb3a268214087a94a70f25c088f5410f800ab6d',
				token: token
			});
		},
		interceptor: ['unsignedException', 'login fail']
	});

	// Case CRUD
	ocAPI.API('createCase', { //OK SUCCESS
		request: function (caseBody) {
			this.cache.remove('CASE_LIST');
			return $http.post(this.APIOrigin + "case/", caseBody);
		},
		interceptor: ['isLogined']
	});
	ocAPI.API('updateCase', {
		request: function (caseId, caseBody) {
			this.cache.remove('CASE_LIST');
			this.cache.remove('CASE_' + caseId);
			return $http.put(this.APIOrigin + "case/" + caseId, caseBody);
		},
		interceptor: ['isLogined']
	});
	ocAPI.API('deleteCase', {
		request: function (caseId) {
			this.cache.remove('CASE_LIST');
			return $http['delete'](this.APIOrigin + "case/" + caseId);
		},
		interceptor: ['isLogined']
	});
	ocAPI.API('retrieveCase', { //OK WARN
		request: function (caseId) {
			return $http.get(this.APIOrigin + "case/" + caseId);
		},
		interceptor: ['isLogined'],
		cacheKey: 'CASE_'
	});
	ocAPI.API('listCase', { //OK WARN
		request: function () {
			return $http.get(this.APIOrigin + "case/list");
		},
		interceptor: ['isLogined'],
		cacheKey: 'CASE_LIST'
	});

	// Dictionary CRUD
	ocAPI.API('createDictionary', { //OK
		request: function (dictionaryBody) {
			this.cache.remove('DICTIONARY_LIST');
			return $http.post(this.APIOrigin + "dictionary/", dictionaryBody);
		},
		interceptor: ['isLogined']
	});
	ocAPI.API('updateDictionary', {
		request: function (dictionaryId, dictionaryBody) {
			this.cache.remove('DICTIONARY_LIST');
			this.cache.remove('DICTIONARY_' + dictionaryId);
			return $http.put(this.APIOrigin + "dictionary/" + dictionaryId, dictionaryBody);
		},
		interceptor: ['isLogined']
	});
	ocAPI.API('deleteDictionary', {
		request: function (dictionaryId) {
			this.cache.remove('DICTIONARY_LIST');
			return $http['delete'](this.APIOrigin + "dictionary/" + dictionaryId);
		},
		interceptor: ['isLogined']
	});
	ocAPI.API('retrieveDictionary', { //OK
		request: function (dictionaryId) {
			return $http.get(this.APIOrigin + "dictionary/" + dictionaryId);
		},
		interceptor: ['isLogined'],
		cacheKey: 'DICTIONARY_'
	});
	ocAPI.API('listDictionary', { //OK
		request: function () {
			return $http.get(this.APIOrigin + "dictionary/list");
		},
		interceptor: ['isLogined'],
		cacheKey: 'DICTIONARY_LIST'
	});

	// Process CRUD
	ocAPI.API('createProcess', {
		request: function (processBody) {
			this.cache.remove('PROCESS_LIST');
			return $http.post(this.APIOrigin + "process/", processBody);
		},
		interceptor: ['isLogined']
	});
	ocAPI.API('updateProcess', {
		request: function (processId, processBody) {
			this.cache.remove('PROCESS_LIST');
			this.cache.remove('PROCESS_' + processId);

			return $http.put(this.APIOrigin + "process/" + processId, processBody);
		},
		interceptor: ['isLogined']
	});
	ocAPI.API('deleteProcess', {
		request: function (processId) {
			this.cache.remove('PROCESS_LIST');
			this.cache.remove('PROCESS_' + processId);

			return $http['delete'](this.APIOrigin + "process/" + processId);
		},
		interceptor: ['isLogined']
	});
	ocAPI.API('retrieveProcess', {
		request: function (processId) {
			return $http.get(this.APIOrigin + "process/" + processId);
		},
		interceptor: ['isLogined'],
		cacheKey: 'PROCESS_$0'
	});
	ocAPI.API('listProcess', {
		request: function () {
			return $http.get(this.APIOrigin + "process/list");
		},
		interceptor: ['isLogined'],
		cacheKey: 'PROCESS_LIST'
	});

	// Object RU
	ocAPI.API('updateObject', {
		request: function (objectBody) {
			this.cache.remove('OBJECT_LIBRARY');
			return $http.put(this.APIOrigin + "object/", objectBody);
		},
		interceptor: ['isLogined']
	});
	ocAPI.API('retrieveObject', {
		request: function () {
			return $http.get(this.APIOrigin + "object/");
		},
		interceptor: ['isLogined'],
		cacheKey: 'OBJECT_LIBRARY'
	});

	// Result
	ocAPI.API('uploadResult', {
		request: function (resultBody) {
			return $http.post(this.APIOrigin + "result/", resultBody);
		},
		interceptor: ['isLogined']
	});

	//INTERCEPTORS
	ocAPI.interceptor('isLogined', function (response) {
		if (response.state === 2) {
			throw "尚未登陆";
		}
	});
});
